package com.sopra.tecnica.poc.server.dao;

import java.math.BigDecimal;

import org.springframework.stereotype.Repository;

import com.sopra.tecnica.poc.server.entity.ItemEntity;

@Repository
public class ItemDao extends ADao<ItemEntity> {
	
	@Override
	protected Class<ItemEntity> getManagedClass() {
		return ItemEntity.class;
	}

	public void updateSumQty(Integer orderNum, Integer lineNum) {
		int sumQty = getJdbcTemplate().queryForInt(
				"SELECT SUM(qtecde - qteann)"
			    + " FROM pub.qtecde"
			    + " WHERE societe = '001'"
			    + " AND noccde = ?"
			    + " AND noclig = ?"
			    + " GROUP BY noclig",
			    orderNum, lineNum);
		
		getJdbcTemplate().update(
				"UPDATE " + getTable()
				+ " SET qtetot = ?"
				+ " WHERE societe = '001'"
				+ " AND noccde = ?"
				+ " AND noclig = ?",
				sumQty, orderNum, lineNum);
		
		System.out.format(
				"UPDATE " + getTable()
				+ " SET qtetot = %s"
				+ " WHERE societe = '001'"
				+ " AND noccde = %s"
				+ " AND noclig = %s%n",
				sumQty, orderNum, lineNum);
	}
	
	public void updateSumPrice(Integer orderNum, Integer lineNum) {
		BigDecimal sumPrice = getJdbcTemplate().queryForObject(
				"SELECT SUM((qtecde - qteann) * prixnet)"
			    + " FROM pub.qtecde"
			    + " WHERE societe = '001'"
			    + " AND noccde = ?"
			    + " AND noclig = ?"
			    + " GROUP BY noclig",
			    BigDecimal.class,
			    orderNum, lineNum);
		
		getJdbcTemplate().update(
				"UPDATE " + getTable()
				+ " SET mnttot = ?"
				+ " WHERE societe = '001'"
				+ " AND noccde = ?"
				+ " AND noclig = ?",
				sumPrice, orderNum, lineNum);
		
		System.out.format(
				"UPDATE " + getTable()
				+ " SET mnttot = %s"
				+ " WHERE societe = '001'"
				+ " AND noccde = %s"
				+ " AND noclig = %s%n",
				sumPrice, orderNum, lineNum);
	}
	
//	public List<ItemEntity> findOrderItems(String num) {
//		GenericQuery genericQuery = initGenericQuery();
//		genericQuery.addToSelect("d.lib");
//		genericQuery.addToFrom("pub.division d");
//		genericQuery.addToWhere(getFullColumnName("societe"), val("001"));
//		genericQuery.addToWhere(getFullColumnName("noccde"), val(num));
//		genericQuery.addToWhere(getFullColumnName("societe"), "d.societe");
//		genericQuery.addToWhere(getFullColumnName("cmarque"), "d.cmarque");
//		genericQuery.addToWhere(getFullColumnName("cdivision"), "d.cdivision");
//		
//		ParameterizedRowMapper<ItemEntity> mapper = new ParameterizedRowMapper<ItemEntity>() {
//			public ItemEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
//				ItemEntity item = getMapper().mapRow(rs, rowNum);
//				item.setDivision(rs.getString("lib"));
//				return item;
//			}
//		};
//		
//		String query = genericQuery.getQuery();
//		System.out.println(query);
//		return getJdbcTemplate().query(query, mapper);
//	}
	
//	public void setColor(ItemEntity item) {
//		String query = "SELECT lib FROM PUB.couleur WHERE societe='001'"
//				+ " AND tcouleur='" + item.getColorType()
//				+ "' AND ccouleur='" + item.getColorCode()
//				+ "' AND cmarque='" + item.getBrandCode()
//				+ "' AND saison='" + item.getSeason()
//				+ "' AND annee='" + item.getYear() + "'";
//		System.out.println(query);
//		item.setColor(getJdbcTemplate().queryForObject(query, String.class));
//	}
}
